package com.gadgets.busi.bm01.service;

import com.alibaba.fastjson.JSON;
import com.gadgets.busi.bm01.common.GadgetsBm01Constant;
import com.gadgets.busi.bm01.pojo.CopyTxtTask;
import com.gadgets.busi.bm01.repository.ICopyTxtTaskRepository;
import com.gadgets.busi.bm01.task.Copy1024ImgRunnable;
import com.gadgets.framework.common.constant.GadgetsConstant;
import com.gadgets.framework.common.repository.ISequeue;
import com.gadgets.framework.common.util.NumberFormatUtil;
import com.gadgets.framework.common.util.ThreadTaskUtil;
import com.gadgets.framework.thread.task.AbstractRunnable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author UncleXiu
 * @version V1.0.0
 * @data 2022-02-15
 */
@Service
@Transactional(transactionManager = "gadgetsDbTransactionManage", rollbackFor = Throwable.class)
public class Img1024CopyService implements IImgCopyService{

    @Autowired
    private ISequeue sequeue;
    @Autowired
    private ICopyTxtTaskRepository copyTxtTaskRepository;

    /**
     * 提交图片拷贝任务
     *
     * @param runnable 线程任务
     */
    @Override
    public void submitImgCopyTask(AbstractRunnable runnable) {
        // 设置任务编号
        runnable.setTaskNo(NumberFormatUtil.formatSqueueNoToTask(
                GadgetsBm01Constant.BM01_MODUEL_NO,
                sequeue.getSequeueNoFromAim(GadgetsConstant.SEQUEUE_NAME_CONSTANT.TASK_NO))
        );
        Copy1024ImgRunnable task = (Copy1024ImgRunnable) runnable;
        // 构建任务信息并保存
        CopyTxtTask copyTxtTask = new CopyTxtTask();
        copyTxtTask.setModuelNo(GadgetsBm01Constant.BM01_MODUEL_NO);
        copyTxtTask.setPercent(0.0);
        copyTxtTask.setTaskNo(runnable.getTaskNo());
        copyTxtTask.setTargetPath(task.getTargetSite());
        copyTxtTask.setPath(JSON.toJSONString(task.getPath()));
        copyTxtTask.setStatus(GadgetsConstant.TASK_STATUS.INIT);
        copyTxtTask.setTaskJson(JSON.toJSONString(runnable));
        // 任务分组1024
        copyTxtTask.setTaskOri(GadgetsBm01Constant.COPY_TASK_ORI.COPY_1024_IMG);
        // 持久化保存拷贝任务
        copyTxtTaskRepository.insertCopyTxtTask(copyTxtTask);
        // 提交线程任务
        ThreadTaskUtil.submit(runnable);
    }

}
